package com.ib.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ib.Server;
import com.ib.client.Client;
import com.ib.domain.DeviceValue;

import io.netty.channel.ChannelHandlerContext;

public class ClientHandler extends CustomHeartbeatHandler {
	
	private static final Logger LOG = LoggerFactory.getLogger(ClientHandler.class);

	private Client client;

	public ClientHandler(Client client) {
		super("client");
		this.client = client;
	}

	@Override
	protected void handleData(ChannelHandlerContext channelHandlerContext, Object msg) {
		String deviceValue = (String) msg;
		LOG.info("client 接收数据:" + deviceValue.toString());

		// DeviceValue s = new DeviceValue();
		// s.setType(TypeData.CUSTOME);
		// s.setSpeed(0);
		// s.setAngle(15);
		// s.setSeatId(TypeData.SERVER_RESPONSE);
		// channelHandlerContext.writeAndFlush(s);
	}

	@Override
	protected void handleAllIdle(ChannelHandlerContext ctx) {
		super.handleAllIdle(ctx);
		sendPing(ctx);
	}

	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		super.channelInactive(ctx);
		client.doConnect();
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		LOG.error(name + " exception" + cause.toString());

	}

}
